package com.hbsi.demo2;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static sun.reflect.misc.ConstructorUtil.getConstructors;

/**
 * @author 程奕红
 * @version 1.0
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class c1=Person.class;
        //对Person类类型进行剖析- - ->都有哪些构造方法、属性、方法
        Constructor[] constructors=c1.getConstructors();//返回一个类型的所有构造方法
        for(Constructor cons:constructors){
            System.out.println(cons);
        }

        System.out.println("==============================================");
        //只要是构造方法
        Constructor[] constructors2=c1.getDeclaredConstructors();
        for(Constructor cons:constructors2){
            System.out.println(cons);
        }
        //拿到的是一个构造器
        System.out.println("---------------------------------------------------");
        Constructor constructor1=c1.getConstructor();
        System.out.println(constructor1);

    Object object=    constructor1.newInstance();//Person  p=new Person();
        System.out.println(object);

        System.out.println("-------------");
        //拿有参的构造
       //Constructor constructor2= c1.getConstructor(String.class);
       // System.out.println(constructor2);

        Constructor constructor2=c1.getDeclaredConstructor(String.class);
        System.out.println(constructor2);
        constructor2.setAccessible(true);
        Object object1=constructor2.newInstance("张三");
        System.out.println(object1);

        System.out.println("......................");
        //不管是什么类型（基本数据类型、引用数据类型都有class对象）
       Constructor constructor3= c1.getDeclaredConstructor(String.class,int.class);
       constructor3.setAccessible(true);
      Object object3= constructor3.newInstance("李四",18);
        System.out.println(object3);
        System.out.println("~~~~~~~~~~~~~~~~~~~~");
        Constructor constructor4= c1.getDeclaredConstructor(String.class,int.class,String.class);
        constructor4.setAccessible(true);
        Object object4= constructor4.newInstance("李四",18,"合肥");
        System.out.println(object4);
    }
}
